﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tuwien.auto.eibclient;

namespace KNXFramework.KNXObjects.ValueObjects
{
    internal class UIntObjectValue : IValueObject
    {
        protected uint m_Value;
        
        #region Constructors
        public UIntObjectValue()
        {

        }
        public UIntObjectValue(uint val)
        {
            m_Value = val;
        }
        public UIntObjectValue(string value)
        {
            if (!UInt32.TryParse(value, out m_Value))
                throw new ApplicationException(string.Format("Format error, cannot convert value {0} to UINT32", value)); 
        }
        public UIntObjectValue(EIBBuffer buffer)
        {

        }
        #endregion

        #region IValueObject Members

        public virtual void AdaptBufferBeforeSending(bool isWrite, EIBBuffer buffer)
        {
            byte[] b = new byte[3];
            b[0] = buffer.data[0];
            b[1] = (byte)(isWrite ? 0x80 : 0x40);
            b[2] = (byte)(m_Value & 0xff);
            buffer.data = b;
        }


        #endregion

        #region IEquatable<IValueObject> Members

        public bool Equals(IValueObject other)
        {
            UIntObjectValue otherVal = other as UIntObjectValue;
            bool equality = false;
            if (otherVal != null)
                equality = m_Value.Equals(otherVal.m_Value);

            return equality;
        }

        #endregion

        public override string ToString()
        {
            return m_Value.ToString();
        }
    }
}
